package pt.inescid.components.policyengine.heimdall.contact;

import java.io.IOException;
import java.net.ServerSocket;

import pt.inescid.components.policyengine.heimdall.commons.Log;
import pt.inescid.components.policyengine.heimdall.commons.ProjectInfo;

public class Inbound extends Thread {
	
    private String hostname = null;
    public int hostport;
    public ServerSocket serverSocket = null;

    
    
    
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      constructor       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public Inbound(String hostname, int hostport) {
    	super("Inbound");
    	
    	this.hostname = hostname;
    	this.hostport = hostport;
    } // Inbound
    
    
    
    
    
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      run       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public void run() {
		Log.debug(Log.INFO, "Arrived.", "Inbound.run");
/* FIXME
		serverSocket = null;
		boolean listening = true;
				
		if (ProjectInfo.isEnabled(ProjectInfo.REMOTE_FEATURE)) {
			
			try {
				serverSocket = new ServerSocket(hostport);
			} catch (IOException e) {
				Log.debug(Log.ERROR, "Could not LISTEN on port " +
						hostport + ": " + e.getMessage(),
				"Inbound.run");
			}
			
			if (serverSocket != null) {
				Log.debug(Log.ERROR, "Listening on port " +
						hostport, "Inbound.run");
				
				try {
					while (listening)
						new InboundThread(serverSocket.accept()).start();
					
					serverSocket.close();
				} catch (IOException e) {
					Log.debug(Log.ERROR, "Error on accepting connection on port " +
							hostport + ": "+  e.getMessage(),
					"Inbound.run");
				}
			}
		}
		*/	
		Log.debug(Log.INFO, "Finishing.", "Inbound.run");
	} // run
    
    
    
    
    
	/* ===========================================================
	==============================================================
	==============================================================
	finalize
	==============================================================
	==============================================================
	=========================================================== */
	protected void finalize() {
		Log.debug(Log.INFO, "Arrived.", "Inbound.finalize");

		if (serverSocket != null) {
			try {
				serverSocket.close();
				
				Log.debug(Log.ERROR, "Released port " +
						hostport, "Inbound.finalize");
			} catch (IOException e) {
				Log.debug(Log.ERROR, "Could not RELEASE port " +
						hostport + ": " + e.getMessage(),
						"Inbound.finalize");
			}
		}
		else {
			Log.debug(Log.ERROR, "Port " +
					hostport  +"was null.", "Inbound.finalize");
		}
	} // finalize

	
	
	
	
	/* ===========================================================
	==============================================================
	==============================================================
	stopInstance
	==============================================================
	==============================================================
	=========================================================== */
	public void stopInstance() {
		Log.debug(Log.INFO, "Finalizing.", "Inbound.stopInstance");

		if (serverSocket != null) {
			try {
				serverSocket.close();
				serverSocket = null;
				
				Log.debug(Log.ERROR, "Released port " +
						hostport, "Inbound.stopInstance");
			} catch (IOException e) {
				Log.debug(Log.ERROR, "Could not RELEASE port " +
						hostport + ": " + e.getMessage(),
						"Inbound.stopInstance");
			}
		}
		else {
			Log.debug(Log.ERROR, "Port " +
					hostport  +"was null.", "Inbound.stopInstance");
		}
	}; // stopInstance


	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      isRunning       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public boolean isRunning() {
    	return (serverSocket != null && !serverSocket.isClosed());
    }
    

}; // Inbound
